// ActionScript file
import com.newland.flow.controller.Controller;
import com.newland.flow.effect.EffectUtil;
import com.newland.flow.menu.NodePropertiesWin;
import com.newland.flow.model.LinkModel;
import com.newland.flow.model.NodeUtils;
import com.newland.flow.model.ProcessModuleLocator;
import com.newland.flow.view.BaseNodeView;
import com.newland.flow.view.EndNodeView;
import com.newland.flow.view.LinkLineView;
import com.newland.flow.view.StartNodeView;
import com.newland.flow.view.TaskNodeView;

import flash.events.MouseEvent;
import flash.geom.Point;

import mx.collections.XMLListCollection;
import mx.controls.Alert;
import mx.controls.Menu;
import mx.controls.MenuBar;
import mx.controls.TextInput;
import mx.core.Container;
import mx.core.DragSource;
import mx.core.UIComponent;
import mx.events.DragEvent;
import mx.managers.DragManager;

[Embed('asserts/figures/start.gif')]
[Bindable]
private static var startIcon:Class;
[Embed('asserts/figures/activity.gif')]
[Bindable]
private static var taskIcon:Class;
[Embed('asserts/figures/end.gif')]
[Bindable]
private static var endIcon:Class;

[Embed('asserts/icon/toolbar/remove.png')]
[Bindable]
private static var removeIconEnable:Class;

[Embed('asserts/icon/toolbar/remove.png')]
[Bindable]
private static var removeIconDisable:Class;

[Bindable]
public var processModule:ProcessModuleLocator = ProcessModuleLocator.getInstance();
private var effect:EffectUtil = new EffectUtil();

private function complete():void {
	menuBarCollection = new XMLListCollection(menubarXML);
	design.addEventListener(MouseEvent.CLICK,designClick);
	design.addEventListener(DragEvent.DRAG_DROP,design_dragDropHandler);
	design.addEventListener(DragEvent.DRAG_ENTER,design_dragEnterHandler);
}
private function designClick(event:MouseEvent):void {
	var target:UIComponent = event.currentTarget as UIComponent;
	var createNode:UIComponent;
	if(processModule.nodeType !=null && processModule.nodeType != "") {
		if(processModule.nodeType == NodeUtils.LINKLINE) {
			/**
			 * 选择两个节点，进行画线
			 * 只有两次都点击在Node节点上，才进行画线操作；
			 * 设置鼠标样式为画线形状
			 **/
			
		} else {
			if(processModule.nodeType == NodeUtils.STARTNODE) {
				if(processModule.nodeCollection != null && processModule.nodeCollection.length > 0) {
					for(var i:Number=0;i<processModule.nodeCollection.length;i++) {
						var obj:UIComponent = processModule.nodeCollection.getItemAt(i) as UIComponent;
						if(BaseNodeView(obj) is StartNodeView) {
							return;
						}
					}
				}
				var startNode:StartNodeView = new StartNodeView(startIcon,NodeUtils.START_NODE_NAME);
				startNode.nodeType = NodeUtils.STARTNODE;
				createNode = startNode as UIComponent;
			}
			if(processModule.nodeType == NodeUtils.TASKNODE) {
				var taskNode:TaskNodeView = new TaskNodeView(taskIcon,NodeUtils.TASK_NODE_NAME);
				taskNode.nodeType = NodeUtils.TASKNODE;
				createNode = taskNode as UIComponent;
			}
			if(processModule.nodeType == NodeUtils.ENDNODE) {
				if(processModule.nodeCollection != null && processModule.nodeCollection.length > 0) {
					for(var j:Number=0;j<processModule.nodeCollection.length;j++) {
						var obj:UIComponent = processModule.nodeCollection.getItemAt(j) as UIComponent;
						if(BaseNodeView(obj) is EndNodeView) {
							return;
						}
					}
				}
				var endNode:EndNodeView = new EndNodeView(endIcon,NodeUtils.END_NODE_NAME);
				endNode.nodeType = NodeUtils.ENDNODE;
				createNode = endNode as UIComponent;
			}
			createNode.id = String(processModule.size);
			createNode.addEventListener(MouseEvent.CLICK,nodeClick);
			createNode.addEventListener(MouseEvent.MOUSE_MOVE,node_mouseMoveHandler);
			createNode.removeEventListener(MouseEvent.ROLL_OUT,rollOut);
			createNode.removeEventListener(MouseEvent.ROLL_OVER,rollOver);
			createNode.addEventListener(MouseEvent.ROLL_OUT,rollOutEffect);
			createNode.addEventListener(MouseEvent.ROLL_OVER,rollOverEffect);
			createNode.useHandCursor = true;
			createNode.buttonMode = true;
			createNode.x = target.mouseX;
			createNode.y = target.mouseY;
			target.addChild(createNode);
			//组件集合管理
			processModule.nodeCollection.addItem(createNode);
			processModule.size = processModule.size + 1;
		}
	}
}
/**
 * 节点选择鼠标移入效果
 **/
private function rollOver(event:MouseEvent):void {
	var target:UIComponent = event.currentTarget as UIComponent;
	target.setStyle("backgroundColor","#9adae8");
}
/**
 * 节点选择鼠标移出效果
 **/
private function rollOut(event:MouseEvent):void {
	var target:UIComponent = event.currentTarget as UIComponent;
	target.setStyle("backgroundColor","#ffffff");
}
/**
 * 节点鼠标移入、移除效果
 **/
private function rollOutEffect(event:MouseEvent):void {
	effect.rollOutEffect(event,[event.currentTarget]);
}
private function rollOverEffect(event:MouseEvent):void {
	effect.rollOverEffect(event,[event.currentTarget]);
}
/**
 * 节点单击事件
 * 该对象为存储两个需要连线的节点对象结合；长度仅为2；当达到的时候，将两个对象画线
 **/
private function nodeClick(event:MouseEvent):void {
	/**
	 * 单击节点，移除图标为生效状态
	 **/
	//processModule.removeEnable = true;
	
	/**
	 * 弹出属性设置框
	 * 当锁定状态为画线时，点击节点的时候，不触发获取属性的动作
	 **/
	if(processModule.nodeType == NodeUtils.LINKLINE)
	{
		var clickObject:UIComponent = event.currentTarget as UIComponent;
		BaseNodeView(clickObject).setStyle("backgroundColor","0x0000ff");//表框颜色，标识选中的节点对象
		processModule.linkObjectCollection.addItem(clickObject);
		/**
		 * 在进行画线前，还需要进行判断
		 * 1、连线不能从任务节点到开始节点；
		 * 2、不能从结束节点到开始节点
		 * 3、不能从结束节点到任务节点
		 * 4、在已经存在有连接线的两个任务节点，再次连线
		 * 5、在已经存在有连接线的两个任务节点，再次做相反的连接
		 * 6、连接的两个节点为自身（同一对象）的时候，提示不予连接
		 **/
		if(processModule.linkObjectCollection.length == 2)
		{
			var startNode:UIComponent = processModule.linkObjectCollection.getItemAt(0) as UIComponent;
			var endNode:UIComponent = processModule.linkObjectCollection.getItemAt(1) as UIComponent;
			/**1\2\3情况的判断**/
			if((startNode is TaskNodeView && endNode is StartNodeView)
				|| (startNode is EndNodeView && endNode is StartNodeView) 
				|| (startNode is EndNodeView && endNode is TaskNodeView)) {
				startNode.clearStyle("backgroundColor");
				endNode.clearStyle("backgroundColor");
				processModule.linkObjectCollection.removeAll();//移除临时对象
				Alert.show(NodeUtils.ERROR_LINE_TIP);
				return;
			}
			/**4\5 情况的判断**/
			if(processModule.lineCollection != null && processModule.lineCollection.length > 0) {
				for(var index:Number=0;index<processModule.lineCollection.length;index++) {
					var link:LinkModel = processModule.lineCollection.getItemAt(index) as LinkModel;
					var sNode:UIComponent = link.preNode;
					var eNode:UIComponent = link.endNode;
					if((sNode == startNode && eNode == endNode) || (sNode == endNode && eNode == startNode)) {
						startNode.clearStyle("backgroundColor");
						endNode.clearStyle("backgroundColor");
						processModule.linkObjectCollection.removeAll();//移除临时对象
						Alert.show(NodeUtils.HAD_LINE_TIP);
						return;
					}
				}
			}
			/**6 情况的判断**/
			if(startNode == endNode) {
				startNode.clearStyle("backgroundColor");
				endNode.clearStyle("backgroundColor");
				processModule.linkObjectCollection.removeAll();//移除临时对象
				return;
			}
			var linkLine:LinkLineView = new LinkLineView();
			linkLine.setStartPoint(new Point(startNode.x+startNode.width/2,startNode.y+startNode.height/2));
			linkLine.setEndPoint(new Point(endNode.x+endNode.width/2,endNode.y+endNode.height/2));
			linkLine.drawLine();
			(startNode.parentDocument.design as UIComponent).addChild(linkLine);
			/**1、通过LinkModel维护开始节点、结束节点的连接关系**/
			var model:LinkModel = new LinkModel();
			model.preNode = startNode;
			model.line = linkLine;
			model.endNode = endNode;
			/**2、在开始节点中，维护它所指向的下一节点关系**/
			if(processModule.nodeCollection.contains(startNode)) {
				var index:Number = processModule.nodeCollection.getItemIndex(startNode);
				var node:BaseNodeView = processModule.nodeCollection.getItemAt(index) as BaseNodeView;
				node.linkToCollection.addItem(endNode);
			}
			//连接完毕，清除选中节点的底色
			startNode.clearStyle("backgroundColor");
			endNode.clearStyle("backgroundColor");
			processModule.lineCollection.addItem(model);//将开始节点、线、结束节点关系放入连接线模型维护
			processModule.linkObjectCollection.removeAll();//移除临时对象
		}
	} else {
		//两次选中的是同一个对象，取消底色
		var clickObject:UIComponent = event.currentTarget as UIComponent;
		if(processModule.clickNode == null) {
			processModule.clickNode = clickObject;
			BaseNodeView(clickObject).setStyle("backgroundColor","0x0000ff");//表框颜色，标识选中的节点对象
			var win:NodePropertiesWin = this.propertis;
			win.init(event.currentTarget as UIComponent);
		} else {
			if(processModule.clickNode == clickObject) {//两次选中的是同一对象
				clickObject.clearStyle("backgroundColor");
				processModule.clickNode = null;
				var win:NodePropertiesWin = this.propertis;
				win.init(null);
			} else {//两次选中的不是同一对象
				processModule.clickNode.clearStyle("backgroundColor");
				BaseNodeView(clickObject).setStyle("backgroundColor","0x0000ff");//表框颜色，标识选中的节点对象
				processModule.clickNode = clickObject;
				var win:NodePropertiesWin = this.propertis;
				win.init(event.currentTarget as UIComponent);
			}
		}
	}
}
//对象拖拽的完整流程
private function node_mouseMoveHandler(event:MouseEvent):void {
	var dragInitiator:UIComponent = event.currentTarget as UIComponent;
	var ds:DragSource = new DragSource();
	ds.addData(dragInitiator,"Component");
	DragManager.doDrag(dragInitiator,ds,event);
}
private function design_dragEnterHandler(event:DragEvent):void {
	if(event.dragSource.hasFormat("Component"))
	{
		DragManager.acceptDragDrop(event.currentTarget as UIComponent);
	}
}
/**
 * 移动对象位置确定后，根据开始节点、连接线、结束节点关系集合，重绘连接线
 **/
private function design_dragDropHandler(event:DragEvent):void {
	var dragObject:UIComponent = event.dragInitiator as UIComponent;
	dragObject.x = Container(event.currentTarget).mouseX - dragObject.width/2;
	dragObject.y = Container(event.currentTarget).mouseY - dragObject.height/2;
	if(dragObject.parent!=event.currentTarget){
		Container(event.currentTarget).addChild(dragObject);
	}
	refresh(dragObject);//移动节点，重绘连接线
}
private function refresh(dragObject:UIComponent):void {
	if(processModule.lineCollection.length > 0)
	{
		for(var i:Number=0;i<processModule.lineCollection.length;i++)
		{
			var model:LinkModel = processModule.lineCollection.getItemAt(i) as LinkModel;
			var lineView:LinkLineView = model.line as LinkLineView;
			if(model.preNode == dragObject)
			{
				lineView.removeLine();
				lineView.setStartPoint(new Point(dragObject.x+dragObject.width/2,dragObject.y+dragObject.height/2));
				lineView.drawLine();
			}
			if(model.endNode == dragObject)
			{
				lineView.removeLine();
				lineView.setEndPoint(new Point(dragObject.x+dragObject.width/2,dragObject.y+dragObject.height/2));
				lineView.drawLine();
			}
		}
	}
}